//
//  FirstViewController.m
//  Emoji
//
//  Created by Bobo on 11-11-15.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "SettingController.h"
#import "PreviewController.h"

#define PREFS_FILE	@"/private/var/mobile/Library/Preferences/com.apple.Preferences.plist"
#define EMOJI_KEY	@"KeyboardEmojiEverywhere"

@implementation SettingController

@synthesize emojiSwitch;
@synthesize textView;
@synthesize switchBgView;
@synthesize segmentedControl;
@synthesize bannerIsVisible;
							
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"预览"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:self
                                                            action:@selector(goToPreview)];
    self.navigationItem.rightBarButtonItem = item;
    [item release];

    self.title = @"Emoji";
    
    self.textView.layer.cornerRadius = 15;
    self.textView.text = @" 1.打开emoji开关,然后退出程序\n 2.进入系统设置 > 通用 > 键盘 > 国际键盘 > 添加新键盘 > 日本\n 3.选择emoji\n 4.点击键盘中的小地球换到emoji键盘享受吧";
    
    self.switchBgView.layer.cornerRadius = 15;
    
    self.emojiSwitch.on = NO;
}

- (void)viewDidUnload
{
    [self setTextView:nil];
    [self setSegmentedControl:nil];
    [self setSwitchBgView:nil];
    [self setEmojiSwitch:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [textView release];
    [segmentedControl release];
    [switchBgView release];
    [emojiSwitch release];
    [super dealloc];
}

#pragma mark - Callback

- (void) goToPreview {
    PreviewController *previewController = [[PreviewController alloc] init];
    [self.navigationController pushViewController:previewController
                                         animated:YES];
    [previewController release];
}

- (IBAction)switchEmoji:(id)sender {
    UISwitch *switchControl = (UISwitch *)sender;
    
    NSMutableDictionary *dict = [NSMutableDictionary
                                 dictionaryWithContentsOfFile:PREFS_FILE];
    
    if (switchControl.on) {
        [dict setObject:[NSNumber numberWithBool:YES] forKey:EMOJI_KEY];
    }
    else {
        [dict removeObjectForKey:EMOJI_KEY];
    }
    [dict writeToFile:PREFS_FILE atomically:NO];
}

- (IBAction)changeSegment:(id)sender {
    UISegmentedControl *control = (UISegmentedControl *)sender;
    switch (control.selectedSegmentIndex) {
        case kIOSVersion3: {
            self.textView.text = @" 1.打开emoji开关,然后退出程序\n 2.进入系统设置 > 通用 > 键盘 > 国际键盘 > 添加新键盘 > 日本\n 3.选择emoji\n 4.点击键盘中的小地球换到emoji键盘享受吧";
        }
            break;
            
        case kIOSVersion4: {
            self.textView.text = @" 1.打开emoji开关,然后退出程序\n 2.进入系统设置 > 通用 > 键盘 > 国际键盘 > 添加新键盘 > 表情符号\n 3.点击键盘中的小地球换到emoji键盘享受吧";
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - ADBannerViewDelegate

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    return YES;
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if (!self.bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
        // Assumes the banner view is just off the bottom of the screen.
        banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
        [UIView commitAnimations];
        self.bannerIsVisible = YES;
    }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    if (self.bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
        // Assumes the banner view is placed at the bottom of the screen.
        banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
        [UIView commitAnimations];
        self.bannerIsVisible = NO;
    }
}

@end
